<template>
	<view class="page_container">
		<view class="page_content">
			<view class="common_card">
				<view class="common-bold-title">{{detail.devName}}</view>
				
				<view class="title_row" style="margin-top: 32rpx;">
					<view class="title_row_key">设备规格：</view>
					<view class="title_row_value">{{detail.devSpecification}}</view>
				</view>
				<view class="title_row" style="margin-top: 24rpx;">
					<view class="title_row_key">SN号：</view>
					<view class="title_row_value">{{detail.serialNumber}}</view>
				</view>
				<view class="title_row" style="margin-top: 24rpx;">
					<view class="title_row_key">生产日期：</view>
					<view class="title_row_value">{{detail.productionDate}}</view>
				</view>
				<view class="title_row" style="margin-top: 24rpx;">
					<view class="title_row_key">质保期限：</view>
					<view class="title_row_value">{{detail.warrantyPeriod}}</view>
				</view>
			</view>
			<view class="common_card scrollable_card">
				<view class="list_content">
					<view class="title_content">
						<view>设备元器件列表</view>
						<view class="title_desc">共有{{components.length}}个元器件</view>
					</view>
				</view>
				<view class="scroll_content" id="scroll_content">
					<scroll-view scroll-y="true" :show-scrollbar="false" enhanced="true" enable-flex="true" style="width: 100%;height: 100%;">
						<view class="component_item_content" id="component_item_content">
							<view class="component_item" v-for="(item,index) in components" :key="index">
								<view class="component_input_row">
									<image src="/static/project/device_type_1.png" mode="aspectFit" style="margin-left: 24rpx;width: 22rpx;height: 28rpx;flex-shrink: 0;"></image>
									<input class="input_css" type="text" v-model="item.componentGoodsName"/>
									<view class="component_type">{{item.componentType}}</view>
								</view>
								<view class="component_line"></view>
								<view class="component_info_row">
									<view class="">设备编号：{{item.componentNumber}}</view>
									<view style="margin-left: 40rpx;">安装位置：{{item.componentLocation}}</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</view>
		</view>
		
		<view class="bottom-view_2" id="bottom-view_2">
			<view class="bottom_btn" @click.stop="confirmAdd">确认加入</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				detail:{
					
				},
				components:[],
				rawInfo:[]
			}
		},
		
		onLoad() {
			let self = this;
			self.$progressHud.showLoading();
			self.$NetApi.getMethod('/loyu-api/app/dev/detail',{
				devId:self.$Route.query.devId
			}).then(ok => {
				self.$progressHud.hideLoading();
				self.detail = ok.data;
				if(self.detail.componentList && typeof self.detail.componentList === 'object') {
					self.components = [].concat(...Object.values(self.detail.componentList));
					self.rawInfo = JSON.parse(JSON.stringify(self.components));
				}
			}, fail => {
				self.$progressHud.hideLoading();
				self.$progressHud.showToast(fail.errMsg || '网络错误');
			})
		},
		methods: {
			deviceTypeImage(item){
				var str = ""
				switch(item.devType) {
					case 1:
					{//--高压/低压预装式
						str = "/static/project/device_type_1.png";
						break;
					}
					case 2:
					{//--低压成套开关设备
						str = "/static/project/device_type_2.png";
						break;
					}
					case 3:
					{//--配电柜
						str = "/static/project/device_type_3.png";
						break;
					}
					case 4:
					{//--动力柜
						str = "/static/project/device_type_4.png";
						break;
					}
					case 5:
					{//--低压无功率补偿装置
						str = "/static/project/device_type_5.png";
						break;
					}
					case 6:
					{//--高压金属铠装移开式开关设备
						str = "/static/project/device_type_6.png";
						break;
					}
					default:{
						str = "";
					}
				}
				return str;
			},
			confirmAdd(){
				const modifiedItems = this.findModifiedElements(this.rawInfo, this.components) || [];
				const obj = modifiedItems.map(item=>{
					return {
						alias:item.componentGoodsName,
						componentGoodsId:item.componentGoodsId
					}
				});
				
				let self = this;
				self.$progressHud.showLoading();
				self.$NetApi.postMethod('/loyu-api/app/dev/add-to-item',{
					devId:self.$Route.query.devId,
					itemId:self.$Route.query.itemId,
					componentAliasList:obj
				}).then(ok => {
					self.$progressHud.hideLoading();
					self.$progressHud.showToast('设备加入成功')
					uni.navigateBack();
				}, fail => {
					self.$progressHud.hideLoading();
					self.$progressHud.showToast(fail.errMsg || '网络错误');
				})
			},
			findModifiedElements(originalArr, modifiedArr) {
			    return modifiedArr.filter((modifiedObj, index) => {
			        const originalObj = originalArr[index];
			        return originalObj.componentGoodsName !== modifiedObj.componentGoodsName;
			    });
			}
		}
	}
</script>

<style lang="scss" scoped>
.page_container {
	position: relative;
	width: 100%;
	height: 100%;
	display: flex;
	flex-direction: column;
	overflow: hidden;
}
.page_content {
		width: 100%;
		height: 100%;
		
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		align-items: flex-start;
		row-gap: 32rpx;
		overflow: hidden;
	}
	.common_card{
		border-radius: 24rpx;
		background-color: #ffffff;
		box-shadow: 0rpx 2rpx 8rpx 0rpx rgba(0, 0, 0, 0.05);
		padding: 32rpx;
		margin-left: 32rpx;
		margin-right: 32rpx;
		width: calc(100% - 64rpx);
		flex-shrink: 0;
		overflow: clip;
		
		display: flex;
		flex-direction: column;
	}
	.title_row{
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
	}
	.common-bold-title{
		font-family: Roboto;
		font-size: 40rpx;
		font-weight: 600;
		line-height: 56rpx;
		letter-spacing: normal;
		color: #000000;
	}
	.title_row_key{
		font-family: Roboto;
		font-size: 28rpx;
		font-weight: normal;
		line-height: 40rpx;
		letter-spacing: normal;
		color: #6B7280;
		width: 140rpx;
	}
	.title_row_value{
		font-family: Roboto;
		font-size: 28rpx;
		font-weight: normal;
		line-height: 40rpx;
		letter-spacing: normal;
		color: #000000;
	}
	.scrollable_card{
		flex: 1; /* 占据剩余空间 */
		display: flex;
		flex-direction: column;
		overflow: hidden; /* 重要：禁止卡片自身滚动 */
	}
	.scroll_content{
		flex: 1;
		overflow: hidden;
	}
	.list_content{
		width: 100%;
		display: flex;
		flex-direction: column;
		margin-bottom: 24rpx;
	}
	.title_content{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		flex-shrink: 0;

		font-family: Roboto;
		font-size: 32rpx;
		font-weight: 500;
		line-height: 48rpx;
		letter-spacing: normal;
		color: #000000;
	}
	.title_desc{
		font-family: Roboto;
		font-size: 24rpx;
		font-weight: normal;
		line-height: 32rpx;
		letter-spacing: normal;
		color: #6B7280;
	}
	.component_content{
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		align-items: flex-end;
	}
	.component_input_row{
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
	}
	.component_item{
		padding: 32rpx;
		background-color: #EFF6FF;
		border-radius: 24rpx;
	}
	.component_info_row{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		
		font-family: Roboto;
		font-size: 24rpx;
		font-weight: normal;
		line-height: 32rpx;
		letter-spacing: normal;
		color: #4B5563;
		margin-top: 18rpx;
	}
	.component_line{
		width: 100%;
		height: 1px;
		background-color: #DFE9FD;
		margin-top: 16rpx;
	}
	.input_css{
		margin-left: 48rpx;
		border-bottom: 1px dashed #D1D5DB;
	}
	.component_type{
		flex-shrink: 0;
		margin-left: 16rpx;
		
		font-family: Roboto;
		font-size: 24rpx;
		font-weight: normal;
		line-height: 32rpx;
		letter-spacing: normal;
		color: #6B7280;
	}
	.component_item_content{
		display: flex;
		flex-direction: column;
		row-gap: 32rpx;
		padding-bottom: calc(env(safe-area-inset-bottom) + 90rpx + 64rpx);
	}
	.bottom-view_2{
		position: absolute;
		left: 0px;
		right: 0px;
		bottom: 0px;
		z-index: 100;
		
		background-color: #FFF;
		padding-left: 32rpx;
		padding-right: 32rpx;
		padding-top: 32rpx;
		padding-bottom: calc(env(safe-area-inset-bottom) + 32rpx);
		
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		column-gap: 32rpx;
	}
	.bottom_btn{
		border-radius: 8rpx;
		background: $power-color-primary;
		width: 100%;
		text-align: center;
		color: #FFF;
		
		
		font-family: Roboto;
		font-size: 28rpx;
		font-weight: normal;
		height: 90rpx;
		line-height: 90rpx;
		letter-spacing: normal;
	}
	.bottom_btn_2{
		border-radius: 8rpx;
		background: #FFF;
		width: 100%;
		text-align: center;
		color: $power-color-primary;
		border: 2rpx solid $power-color-primary;
		
		font-family: Roboto;
		font-size: 28rpx;
		font-weight: normal;
		height: 90rpx;
		line-height: 90rpx;
		letter-spacing: normal;
	}
</style>
